package com.compass.doctor.ui.home.fragment

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.compass.doctor.databinding.FragmentBaseListBinding
import com.compass.doctor.ui.home.adapter.ArticleAndVideoAdapter
import com.compass.doctor.ui.home.viewmodel.KnowledgeCenterViewModel
import com.compass.doctor.base.BaseVBFragment
import com.compass.doctor.ui.WebDetailActivity
import com.compass.framework.R
import com.compass.framework.constant.ARTICLE_DETAILS
import com.compass.framework.constant.KEY_CONSULT_TYPE
import com.compass.framework.constant.KEY_ORDER_TYPE
import com.compass.framework.constant.KEY_PATIENT_ID
import com.compass.framework.constant.VIDEO_DETAILS
import com.compass.framework.decoration.NormalItemDecoration
import com.compass.framework.dialog.CommonMessageDialog
import com.compass.framework.ext.gone
import com.compass.framework.ext.load
import com.compass.framework.ext.visible
import com.compass.framework.utils.dpToPx
import com.compass.framework.utils.getStringFromResource
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * Description： 文章专栏Fragment
 * Date:2023/12/26 14:28
 */
class ArticleFragment : BaseVBFragment<FragmentBaseListBinding>(), OnLoadMoreListener,
    OnRefreshListener {

    private val knowledgeCenterViewModel: KnowledgeCenterViewModel by viewModels()

    private lateinit var mAdapter: ArticleAndVideoAdapter
    private var mPage = 1
    private var keyword: String? = null
    private var comeType: Int = 1
    private var patientId:String?=null
    override fun initView(view: View, savedInstanceState: Bundle?) {
        arguments?.let {
            comeType = it.getInt(KEY_ORDER_TYPE)
            patientId = it.getString(KEY_PATIENT_ID)
        }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        mBinding?.refreshLayout?.apply {
            setEnableRefresh(true)
            setEnableLoadMore(true)
            setOnRefreshListener(this@ArticleFragment)
            setOnLoadMoreListener(this@ArticleFragment)
        }
        mAdapter = ArticleAndVideoAdapter(2)
        mAdapter.setArticleOrVideoType(1)
        mBinding?.recyclerView?.apply {
            layoutManager = LinearLayoutManager(context)
            val dp10 = dpToPx(10)
            addItemDecoration(NormalItemDecoration().apply {
                setBounds(top = dp10, left = dp10, right = dp10, bottom = dp10)
                setLastBottom(true)
            })
            adapter = mAdapter
        }

        mAdapter.onItemClickListener = { view, position ->
            val item = mAdapter.getItem(position)
            item?.let { details->
                if (comeType==1){
                    WebDetailActivity.start( requireActivity(),ARTICLE_DETAILS + details.id,details.title )
                }else{
                    patientId?.let { id->
                        CommonMessageDialog.Builder(requireActivity())
                            .setTitle(getStringFromResource(R.string.dialog_tips_title))
                            .setMessage("是分享当前科普文章给患者？")
                            .setConfirm(getStringFromResource(R.string.default_confirm))

                            .setCancel(getString(com.compass.framework.R.string.default_cancel))
                            .setonCancelListener {
                                it?.dismiss()
                            }
                            .setonConfirmListener {
                                knowledgeCenterViewModel.articleAndVideoShare(details.id,id)
                                it?.dismiss()
                            }.create().show()
                    }
                }

            }
        }

    }

    fun searchArticle(key: String?) {
        keyword = key
        mPage = 1
        getArticlePage()
    }

    override fun initData() {
        knowledgeCenterViewModel.articleLiveData.observe(this) {
            if (mPage == 1) {
                mAdapter.setData(it)
                mBinding?.refreshLayout?.finishRefresh()
                mBinding?.refreshLayout?.setEnableLoadMore(load(it?.size))
                if (it.isNullOrEmpty()) {
                    mBinding?.emptyView?.visible()
                } else {
                    mBinding?.emptyView?.gone()
                }
            } else {
                mBinding?.refreshLayout?.finishLoadMore()
                if (!it.isNullOrEmpty()) {
                    mAdapter.addAll(it)
                } else {
                    mBinding?.refreshLayout?.setEnableLoadMore(false)
                }
            }
        }
        knowledgeCenterViewModel.shareLiveData.observe(this){
            it?.let {
                showToast("分享成功")
                activity?.finish()
            }
        }
        getArticlePage()
    }


    private fun getArticlePage() {
        if (comeType==1){
            knowledgeCenterViewModel.getArticlePage(mPage, keyword,"1")
        }else{
            knowledgeCenterViewModel.getArticlePage(mPage, keyword)
        }

    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        mPage++
        getArticlePage()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mPage = 1
        getArticlePage()
    }
}